//
//  MYGNavigationController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/2/23.
//

import UIKit
import QMUIKit

class MYGNavigationController: UINavigationController {
    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        
//        self.navigationBar.clipsToBounds = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private var backButtonItem: UIBarButtonItem {
        return UIBarButtonItem(customView: backButton)
    }
    
    private lazy var backButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle(" ", for: .normal)
        btn.spacingBetweenImageAndTitle = 4
        btn.imagePosition = .left
        btn.setImage(UIImage(named: "back"), for: .normal)
        btn.imageView?.tintColor = .rgba(51, 51, 51, 1)
        btn.addTarget(self, action: #selector(backVc), for: .touchUpInside)
        return btn
    }()
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
        
//        if viewController.navigationItem.leftBarButtonItem == nil && viewControllers.count > 1 {
//            viewController.navigationItem.leftBarButtonItem = backButtonItem
//        }
//        
        if let vc = self.parent as? MYGTabBarController, viewControllers.count > 1 {
            self.view.frame = UIScreen.main.bounds
            vc.hiddenButton = true
        }
        
    }
    public override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
//        self.navigationBar.barTintColor = .white
        self.navigationBar.isTranslucent = false
        if #available(iOS 13.0, *) {
            let newAppearance = UINavigationBarAppearance()
            newAppearance.configureWithDefaultBackground()
            newAppearance.backgroundColor = .white
            newAppearance.shadowColor = UIColor.white
            newAppearance.backgroundImage = UIImage()
            newAppearance.shadowImage = UIImage()
            newAppearance.backgroundEffect = nil
            newAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.medium(18)]
            UINavigationBar.appearance().standardAppearance = newAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = newAppearance
        }
       
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        let vc = super.popViewController(animated: animated)
        
        if let vc = self.parent as? MYGTabBarController, viewControllers.count <= 1 {
            self.view.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight-tabbarHeight-bottomSafeheight)
            vc.hiddenButton = false
        }
        
        return vc
    }
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        let vcs = super.popToRootViewController(animated: animated)
        if let vc = self.parent as? MYGTabBarController, viewControllers.count >= 1 {
            self.view.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight-tabbarHeight-bottomSafeheight)
            vc.hiddenButton = false
        }
        return vcs
    }
    
    // 是否支撑主动转屏
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? false
    }
    // 支撑哪些屏幕方向
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? .portrait
    }
    // 默许的屏幕方向（当时ViewController有必要是经过模态出来的UIViewController（模态带导航的无效）办法展现出来的，才会调用这个办法）
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return topViewController?.preferredInterfaceOrientationForPresentation ?? .portrait
    }
    
}

extension MYGNavigationController {
    @objc
    func backVc() {
        self.popViewController(animated: true)
    }
}
